//
// Created by 高森森 on 2022/9/27.
//

#ifndef LEETCODE_SOLUTION_9_H
#define LEETCODE_SOLUTION_9_H
#include<bits/stdc++.h>
using namespace std;

class solution_9 {
public:
    int hIndex(vector<int>& citations) {
        sort(citations.begin(),citations.end());
        int n=citations.size()-1;
        int h=0;
        while(n>=0&&citations[n]>h){
            h++;
            n--;
        }
        return h;
    }
    int hIndex2(vector<int>& citations) {
        int n=citations.size();
        int sum=0;
        int counter[n+1];
        fill(counter,counter+n+1,0);
        for(int i=0;i<n;i++){
            if(citations[i]>=n){
                counter[n]++;
            }else{
                counter[citations[i]]++;
            }
        }
        for(int i=n;i>=1;i--){
            sum+=counter[i];
            if(sum>=i){
                return i;
            }
        }
        return 0;
    }
};


#endif //LEETCODE_SOLUTION_9_H
